package cn.com.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionSupport;

import cn.com.biz.ConsumerBiz;
import cn.com.entity.Consumer;

public class ConsumerAction extends ActionSupport implements SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//增加功能，包括了用户增加（注册），医生的增加，科室的增加，新闻的增加，公告的增加，挂号单的增加，体检单增加
	private Consumer consumer;
	private ConsumerBiz consumerbiz;
	private String consumermsg;
	private int consumerid;
	private Map<String, Object> session;
	
	public Map<String, Object> getSession() {
		return session;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		session=arg0;
		
	}
	
	public int getConsumerid() {
		return consumerid;
	}
	public void setConsumerid(int consumerid) {
		this.consumerid = consumerid;
	}
	public String getConsumermsg() {
		return consumermsg;
	}
	public void setConsumermsg(String consumermsg) {
		this.consumermsg = consumermsg;
	}
	public Consumer getConsumer() {
		return consumer;
	}
	public void setConsumer(Consumer consumer) {
		this.consumer = consumer;
	}
	
	public ConsumerBiz getConsumerbiz() {
		return consumerbiz;
	}
	public void setConsumerbiz(ConsumerBiz consumerbiz) {
		this.consumerbiz = consumerbiz;
	}
	//注册增加
	public String addConsumer(){
		@SuppressWarnings("resource")
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		consumerbiz=(ConsumerBiz)ctx.getBean("consumerbiz");
//		Map session=ActionContext.getContext().getSession();//成功就把这个用户存到session中
		boolean b=consumerbiz.addConsumer(consumer);
		if(b){
			
			return SUCCESS;
		}else{
			setConsumermsg("注册失败,请重新注册");
			return ERROR;
		}
	}
	//验证用户
	public String consumerLogin(){
		@SuppressWarnings("resource")
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		consumerbiz=(ConsumerBiz)ctx.getBean("consumerbiz");
		Consumer consumerlogin=consumerbiz.Consumerlogin(consumer);
		if(consumerlogin!=null){
			session.put("consumerlogin", consumerlogin);//将登陆的用户存到session中
			return SUCCESS;
		}else{
			setConsumermsg("登录失败，密码或者用户名错误，请您重新登录！");
			return INPUT;
		}
	}
	//修改个人资料
	public String updateConsumer(){
		@SuppressWarnings("resource")
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		consumerbiz=(ConsumerBiz)ctx.getBean("consumerbiz");
		boolean b=consumerbiz.updateConsumer(consumer);
		if(b){
			setConsumermsg("更新成功");
			return SUCCESS;
		}else{
			setConsumermsg("更新失败");
			return ERROR;
		}
		
	}
	//查看个人
	public String SelectOneConsumer(){
		@SuppressWarnings("resource")
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		consumerbiz=(ConsumerBiz)ctx.getBean("consumerbiz");
		Consumer consumerone=consumerbiz.selectOneConsumer(consumer);
		session.put("consumerone",consumerone);	
		return SUCCESS;
	}
	
	
	
	
	
/*	public String selectConsumer(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		consumerbiz=(ConsumerBiz)ctx.getBean("consumerbiz");
		consumer=consumerbiz.selectConsumer(consumerid);
		Map session=ActionContext.getContext().getSession();
		if(consumer!=null){
			session.put("consumernew", consumer);
			return SUCCESS;
		}else{
			setConsumermsg("没有查询到");
			return ERROR;
		}
	}*/
}
